package com.module;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ThreadPoolDemo {
	 static int num=0;
	public static void main(String[] args) {
//		Executor executor = Executors.newFixedThreadPool(100);  
		/*Executor executor = Executors.newWorkStealingPool();
		Runnable runnable =new Runnable() {
			@Override
			public void run() {
				for(int i=0;i<10;i++) {
					System.out.println("WWWWWWWWWW"+i);
				}
				
			}
		};
		Runnable runnable2 =new Runnable() {
			@Override
			public void run() {
				for(int i=0;i<10;i++) {
					System.out.println("WWW2"+i);
				}
				
			}
		};
		executor.execute(runnable);
		executor.execute(runnable2);
		executor.execute(runnable);
		executor.execute(runnable2);
		System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwww");*/
//		Executor exec = Executors.newCachedThreadPool();
//		Executor exec = Executors.newFixedThreadPool(2);	//2~100 如果括号值为1,则保证了，只能有一条线程运行。对线程数量有很大限制！
//		Executor exec = Executors.newSingleThreadExecutor();  //1~100 只能有一条线程运行
//		Executor exec = Executors.newScheduledThreadPool(10); //10条核心线程，最大线程总数。可定时执行。适用于需要多个后台线程执行周期任务的场景。
//		Executor exec = Executors.newSingleThreadScheduledExecutor(); //只有一条核心线程，可定时执行
		Executor exec = Executors.newWorkStealingPool(6);
		System.out.println("几核:"+Runtime.getRuntime().availableProcessors());
		/*try {
			((ExecutorService) exec).awaitTermination(1, TimeUnit.SECONDS);//1分
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/
		 for(int i=0;i<100;i++) {
			Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						TimeUnit.MICROSECONDS.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					num++;
					System.out.println(num);
				}
			});
//			thread.start();
			exec.execute(thread);
			
		}
	}
}
